#include "kdcmovie.h"

#include <kdclib.h>

#include <KLocale>
#include <KIcon>

#include <QFile>

#include <KIcon>

KDCMovie::KDCMovie(	KDCMovieMetaData *aMovieMetaData, KDCObject *aParent ) : KDCObject( KDC::Movie, aParent ), pMovieMetaData( aMovieMetaData ), pChapter( NULL ), pFps( 0 )
{
	pVideo = new KDCVideo( pMovieMetaData->videoMetaData() );
	for( int i=0; i<pMovieMetaData->audioMetaData().size(); ++i )
		pAudio.append( new KDCAudio( pMovieMetaData->audioMetaData().at( i ) ) );	
	pOutputName = baseFileName( pMovieMetaData->name() );
	checkSubtitles();
	pChapter = new KDCChapter( this );
	pSubtitles = new KDCSubtitle( this );
}

KDCMovie::~KDCMovie()
{
	deletePtr( pVideo );
	deletePtr( pChapter );
	deletePtr( pMovieMetaData );
	deletePtr( pSubtitles );
	int i;
	for( i=0; i<pAudio.size(); ++i )
		deletePtr( pAudio.at( i ) );
	for( i=0; i<pSubMovies.size(); ++i )
		deletePtr( pSubMovies.at( i ) );
}

KIcon KDCMovie::icon() const
{
	return KIcon("applications-multimedia");
}

QString KDCMovie::title() const
{
	return pOutputName;
}
	
void KDCMovie::copySettings( const KDCMovie& aMovie )
{
	pFps = aMovie.fps();
	pVideoFormat = aMovie.videoFormat();
	pVideo->copySettings( *aMovie.video() );
	for( int i=0; i<pAudio.size(); ++i )
		pAudio.at( i )->copySettings( *aMovie.audio().at( i ) );
}

KDCChapter* KDCMovie::chapter() const
{
	return pChapter;
}

void KDCMovie::checkSubtitles()
{
	QStringList subs;
	QString str = fileNameWithoutExtension( metaData()->name() );
	if ( QFile::exists( str ) )
		subs << str;
	if ( QFile::exists( str  + ".txt" ) )
		subs << str + ".txt";
	if ( QFile::exists( str  + ".sub" ) )
		subs << str + ".sub";
	if ( QFile::exists( str  + ".srt" ) )
		subs << str + ".srt";

	//for( int i=0; i<subs.size(); ++i )
		//pSubtitle.append( new KDCSubtitle( subs.at( i ), metaData()->videoMetaData()->fps(), this ) );
}

void KDCMovie::setFps( double aFps )
{
	pFps = aFps;
}

double KDCMovie::fps() const
{
	return pFps;
}

KDCVideo* KDCMovie::video() const
{
	return pVideo;
}

QList<KDCAudio*> KDCMovie::audio() const
{
	return pAudio;
}

void KDCMovie::setVideoFormat( KDC::VideoFormat aVideoFormat )
{
	pVideoFormat = aVideoFormat;
}

KDC::VideoFormat KDCMovie::videoFormat() const
{
	return pVideoFormat;
}

quint64 KDCMovie::size() const
{
	quint64 res = 0;
	res = pVideo->size();
	int i;
	for( i=0; i<pAudio.size(); ++i )
	{
		if ( pAudio.at( i )->isEnabled() )
			res += pAudio.at( i )->size();
	}
	for( i=0; i<pSubMovies.size(); ++i )
		res += pSubMovies.at( i )->size();
	return res;
}

bool KDCMovie::operator!=( const KDCMovie& aMovie )
{
	return false;
}

QString KDCMovie::outputName() const
{
	return pOutputName;
}

void KDCMovie::setOutputName( const QString& aName )
{
	pOutputName = aName;
}

int KDCMovie::length() const
{
	int size = metaData()->videoMetaData()->length();
	for( int i=0; i<pSubMovies.size(); ++i )
		size += pSubMovies.at( i )->metaData()->videoMetaData()->length();
	return size;
}

KDCMovieMetaData* KDCMovie::metaData() const
{
	return pMovieMetaData;
}

void KDCMovie::addSubMovie( KDCMovie *aMovie )
{
	aMovie->setParent( this );
	pSubMovies.append( aMovie );
	QList<KDCMovie*> movies = aMovie->subMovies();
	for ( int i=0; i<movies.size(); ++i )
	{
		aMovie->removeSubMovie( i );
		pSubMovies.append( movies.at( i ) );
		pSubMovies.last()->setParent( this );
	}
}

void KDCMovie::removeSubMovie( int aIndex )
{
	if ( isInRange( aIndex, 0, pSubMovies.size() - 1 ) )
	{
		pSubMovies.at( aIndex )->setParent( NULL );
		pSubMovies.removeAt( aIndex );
	}
}

void KDCMovie::moveSubMovie( int aFrom, int aTo )
{
	if ( isInRange( aFrom, 0, pSubMovies.size() - 1 ) && isInRange( aTo, 0, pSubMovies.size() - 1 ) )
		pSubMovies.move( aFrom, aTo );
}

const QList<KDCMovie*>& KDCMovie::subMovies() const
{
	return pSubMovies;
}

KDCSubtitle* KDCMovie::subtitles() const
{
	return pSubtitles;
}
